import cloud from '@lafjs/cloud'

export default async function (ctx: FunctionContext) {
  let now = Date.now()//当前时间
  let { account, password } = ctx.body
  let resp = await cloud.nw.db.findByWhereJson({
    dbName: "admin-users",
    whereJson: { account }
  });
  if (resp) {
    if (resp.password == password) {
      const payload = {
        uid: resp._id,
        exp: Math.floor(now / 1000) + 60 * 60 * 24 * 90,
      }
      const token = cloud.getToken(payload)
      return cloud.utils.requestReturn({ data: token })
    } else {
      return cloud.utils.requestReturn({ code: 500, msg: "输入的密码不对" })
    }
  } else {
    return cloud.utils.requestReturn({ code: 500, msg: "输入的账号不对" })
  }
} 
